package
{
	import away3d.containers.View3D;
	import away3d.lights.DirectionalLight;
	import away3d.lights.LightBase;
	import away3d.lights.PointLight;
	import away3d.materials.ColorMaterial;
	import away3d.materials.methods.SoftShadowMapMethod;
	import away3d.primitives.Cube;
	import away3d.tools.HoverDragController;
	
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Vector3D;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	[SWF(frameRate="60",width="800", height="800")]
	public class Main3D extends Sprite
	{
		
		private var partie:Tetris3D;
		
		private var next:NextBlock;
		
		public static var isEmergency:Boolean = false;
		public static var isLoosing:Boolean = false;
		
		public static var score:TextField;
		
		// 3D
		private var _view : View3D;
		private var _light : DirectionalLight;
		private var _light2 : PointLight;
		private var _light3 : LightBase;
		
		public static var lights:Array;
		
		public static var shadow:SoftShadowMapMethod;
		
		private var camController : HoverDragController; // controle de caméra
		
		
		public function Main3D()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);	
			
			// on s'occupe de la scène 3D
			_view = new View3D;
			_view.antiAlias = 2;
			_view.x = 0;
			_view.y = 0;
			//camController = new HoverDragController(_view.camera, stage);
			_view.camera.rotationX = 180;
			_view.camera.z = 600;
			_view.camera.x = 150;
			_view.camera.y = 250;
			_view.backgroundColor = 0x888888;
			
			
			//camController.radius = 500;
			// Emergency light
			_light = new DirectionalLight(0, -50, -50); // DirectionalLight();
			_light.lookAt(new Vector3D(0,0,0));
			_light.x = 0;
			_light.y = 250;
			_light.z = 500;
			_light.specular = 5;
			_light.diffuse = 0.2;
			//			_light.radius = 2000;
			//			_light.fallOff = 3000;
			_light.color = 0xff1122;
			
			_light2 = new PointLight(); // DirectionalLight();
			_light2.x = 1250;
			_light2.y = 500;
			_light2.z = 200;
			//			_light2.radius = 2000;
			//			_light2.fallOff = 3000;
			_light2.color = 0xffffFF;
			
			_light3 = new DirectionalLight(20, 250, -80);
			_light3.x = 125;
			_light3.y = 0;
			_light3.z = 100;
			_light3.specular = 0;
			_light3.diffuse = 10;
			_light3.color = 0xffffff;
			
			/*var cube:Cube = new Cube(new ColorMaterial(0xFFFF00), 20, 20, 20);
			cube.x = _light3.x;
			cube.y = _light3.y;
			cube.z = _light3.z;
			_view.scene.addChild(cube);*/
			
			// ombre
			shadow = new SoftShadowMapMethod(_light3);
			shadow.stepSize = 5E-4;
			
			lights = [_light3, _light2, _light];
			
			_view.scene.addChild(_light);
			_view.scene.addChild(_light2);
			_view.scene.addChild(_light3);
			this.addChild(_view);
		}
		
		private function onAddedToStage(event:Event):void {
			
			partie = new Tetris3D(stage, ((Main3D) (this)));	
			partie.addEventListener("blockChanged", blockChanged);
			_view.scene.addChild(partie);
			
			next = new NextBlock(partie);
			next.x = Tetris3D.LARGEUR_GRILLE_PX + 40;
			_view.scene.addChild(next);
			
			score = new TextField();
			score.text = "0";
			score.selectable = false;
			var tf:TextFormat = new TextFormat("Arial", 25, 0xFFFF00, true);
			score.setTextFormat(tf);
			score.x = 550;
			score.y = 100;
			addChild(score);
			
			
			var tweenerButton:Sprite = new Sprite();
			tweenerButton.graphics.beginFill(0x45845A);
			tweenerButton.graphics.drawEllipse(0,0,100,45);
			tweenerButton.graphics.endFill();
			tweenerButton.buttonMode = true;
			tweenerButton.mouseChildren = false;
			var tef:TextField = new TextField();
			tef.text = "use tweener";
			tef.setTextFormat(new TextFormat("Arial", 15, 0xFFFFFF, true));
			tef.selectable = false;
			tef.autoSize = TextFieldAutoSize.CENTER;
			tef.x = tweenerButton.width/2 - tef.width/2;
			tef.y = tweenerButton.height/2 - tef.height/2 - 2;
			tweenerButton.addChild(tef);
			
			tweenerButton.addEventListener(MouseEvent.CLICK, switchTweener);
			
			tweenerButton.x = 550;
			tweenerButton.y = 300;
			addChild(tweenerButton);
			
			
			
			// rotate
			var rotateButton:Sprite = new Sprite();
			rotateButton.graphics.beginFill(0x45845A);
			rotateButton.graphics.drawEllipse(0,0,100,45);
			rotateButton.graphics.endFill();
			rotateButton.buttonMode = true;
			rotateButton.mouseChildren = false;
			var tf2:TextField = new TextField();
			tf2.selectable = false;
			tf2.text = "rotate light";
			tf2.autoSize = TextFieldAutoSize.CENTER;
			tf2.x = rotateButton.width/2 - tf2.width/2;
			tf2.y = rotateButton.height/2 - tf2.height/2 - 2;
			rotateButton.addChild(tf2);
			rotateButton.visible = false;
			
			rotateButton.addEventListener(MouseEvent.MOUSE_DOWN, rotateViewOk);
			rotateButton.addEventListener(MouseEvent.MOUSE_UP, rotateViewPasOk);
			
			
			rotateButton.x = 550;
			rotateButton.y = 400;
			addChild(rotateButton);
			
			this.addEventListener(Event.ENTER_FRAME, handleEnterFrame);
			
		}
		
		public static function getLigths():Array {
			return lights;
		}
		
		private function blockChanged(event:Event):void {
			next.gb = partie.nextGBlock;
		}
		
		private function switchTweener(event:MouseEvent):void {
			partie.USER_TWEENER = !partie.USER_TWEENER;
		}
		
		private function handleEnterFrame(event:Event):void {
			if (isEmergency) {
				/*if (!lights.indexOf(_light) == -1 ) {
					lights.push(_light);
				}*/
				_light.rotationY += 12;				
			} else {
				/*if (!lights.indexOf(_light) != -1 ) {
					lights.pop();
				}*/
			}
			
			if (!isLoosing) {
				_view.render();				
			} else {
				var textLoose:TextField = new TextField();
				var tf:TextFormat = new TextFormat("Arial", 72, 0xFFFF00, true);
				textLoose.autoSize = TextFieldAutoSize.CENTER;
				textLoose.text = "GAME OVER !";
				textLoose.setTextFormat(tf);
				textLoose.selectable = false;
				textLoose.x = 150;
				textLoose.y = 400;
				addChild(textLoose);
				
				this.removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
			}
			
		}
		
		private function rotateViewOk(event:MouseEvent):void {
			isEmergency = !isEmergency;
		}
		
		private function rotateViewPasOk(event:MouseEvent):void {
			//isRotateLeft = false;
		}
		
	}
}